package com.twu.biblioteca.controllers;

import com.twu.biblioteca.config.MenuOption;
import com.twu.biblioteca.config.Message;
import com.twu.biblioteca.models.entities.Menu;
import com.twu.biblioteca.models.services.UserService;
import com.twu.biblioteca.views.MenuView;
import com.twu.biblioteca.views.MessageView;
import com.twu.biblioteca.views.UserView;

public class UserController {

    private static int loginCount = 0;

    public static void login() {
        loginCount += 1;

        String name = UserView.getEnterdUserName();
        String password = UserView.getEnterdUserPassword();

        if (UserService.login(name, password)) {
            MessageView.showNotice(Message.LOGIN_SUCCESS, true);
            Menu.updateMenuOptionAccorodingUserLogin();
            MenuView.render();
        } else {
            MessageView.showNotice(Message.LOGIN_UNSUCCESS, true);
            if (loginCount < 3) {
                UserController.login();
            } else {
                MenuView.render();
            }
        }
    }

    public static void show() {
        if (UserService.currentUser != null) {
            UserView.show(UserService.currentUser);
            MenuView.render();
        } else {
            UserController.login();
        }
    }
}
